﻿using System;
using System.Web.Security;
using Common.Constants;
using Entities;
using Business;

namespace ServicesDirectory.Shares
{
    public partial class MainMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;

            var oInfo = new System.IO.FileInfo(sPath);

            if (!oInfo.Name.Contains("ServiceDetail.aspx"))
            {
                LogDataBusiness.Instance.Delete(Session.SessionID);
            }


            if (Session["UserInfo"] == null)
            {
                var cookie = Request.Cookies["usercookie"];
                if (cookie != null)
                {
                    var userBusiness = new UserBusiness();
                    var user = userBusiness.GetUser(cookie.Value);
                    var role = Enum.GetName(typeof(Role), Role.Normal);
                    if (FormsAuthentication.Authenticate(role, role))
                    {
                        Session["UserInfo"] = user;
                        FormsAuthentication.RedirectFromLoginPage(role, false);
                    }

                }
                else
                {
                    FormsAuthentication.RedirectToLoginPage();
                }
            }
            else
            {
                if (!IsPostBack)
                {
                    var user = (User)Session["UserInfo"];
                    lblWelcome.Text += user.Account + " ,<a class=\"logOut\" href='../LogOn.aspx?fn=logout'>logout</a>";
                }
            }
        }
    }
}